const webpack = require('webpack')
const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebPackPlugin = require('html-webpack-plugin')
const { config } = require('process')
const { copyFile } = require('fs')

module.exports = (env, argv) => {
  const config = {
    entry: './src/main.js',
    mode: 'none',
    output: {
      filename: 'bundle.js',
      path: path.join(__dirname, 'dist'),
      publicPath: 'dist/'
    },
    devServer: {
      hot: true,
      contentBase: './public',
    },
    module: {
      rules: [
        {
          test: /.css$/,
          use: [
            'style-loader',
            'css-loader'
          ]
        },
        {
          test: /.png$/,
          use: ['file-loader']
        }
      ]
    },
    plugins: [
      new CleanWebpackPlugin(),
      new HtmlWebPackPlugin({
        title: 'webpack plugin sample',
        meta: {
          viewport: 'width=device-width'
        }
      }),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.DefinePlugin({
        API_BASE_URL: '"http://www.baidu.com"'
      })
    ]
  }
  if (env === 'production') {
    config.mode = 'production'
    config.devtool = false
    config.plugins = [
      ...config.plugins,
      new CleanWebpackPlugin(),
      new CopyWebPackPlugin(['public'])
    ]
  }

  return config
}
